package com.tgram.android_mapapi.Util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

import com.esri.core.geometry.Point;

/**
 * SharedPreferences工具类
 * 
 * @author yongming
 * @time 2015年10月16日16:35:57
 */
public class UtilPreference {

	private static final String TAG = UtilPreference.class.getName();

	private static UtilPreference INSTANCE;
	private static Context mContext;
	private static SharedPreferences preference;

	// 用户信息相关
	private static String USER_KEY = "com.tgram.user.key";
	private static final String USER_CODE = "com.tgram.user.code";
	private static final String USER_GXDWID="com.tgram.user.gxdwid";
	private static final String USER_PCSDM="com.tgram.user.pcsdm";
private static final String  USER_NAME="com.tgram.user.username";
	private static final String USER_PRIVILEGID="com.tgram.user.privilegid";
	// GPS定位坐标相关
	private static String GPS_POINT_KEY = "com.tgram.gps.point.key"; // 键
	private static String GPS_POINT_LX = "com.tgram.gps.point.lx"; // X轴坐标
	private static String GPS_POINT_LY = "com.tgram.gps.point.ly"; // Y轴坐标

	/** longitude **/
	private static final String LOCATION_KEY_LONGITUDE = "location_key_longitude";
	/** latitude **/
	private static final String LOCATION_KEY_LATITUDE = "location_key_latitude";
	
	private static final String MAP_ZOOM_LEVLE_KEY = "map_zoom_levle_key";
	private static String MAP_ZOOM_LEVLE_VALUE = "map_zoom_levle_value";

	private UtilPreference(Context context) {
		mContext = context;

	}

	/**
	 * 获取单例
	 * 
	 * @param context
	 * @return
	 * @author 
	 */
	public static UtilPreference getInstanceUtilPreference(Context context) {
		if (INSTANCE == null) {
			INSTANCE = new UtilPreference(context);
		}
		return INSTANCE;
	}
	/**
	 * 设置参数的KEY和Value
	 * 
	 * @param key
	 * @param obj
	 */
	public void setAttr(String key, Object obj) {
		preference = PreferenceManager.getDefaultSharedPreferences(mContext);
		if (obj instanceof String) {
			String value = (String) obj;
			INSTANCE.setStringAttr(key, value);
		} else if (obj instanceof Boolean) {
			Boolean value = (Boolean) obj;
			INSTANCE.setBooleanAttr(key, value);
		} else if (obj instanceof Integer) {
			Integer value = (Integer) obj;
			INSTANCE.setIntAttr(key, value);
		} else if (obj instanceof Long) {
			Long value = (Long) obj;
			INSTANCE.setLongAttr(key, value);
		}
	}
	private void setStringAttr(String key, String value) {
		SharedPreferences.Editor edit = preference.edit();
		if (value == null) {
			edit.remove(key);
		} else {
			edit.putString(key, value);
		}
		edit.commit();
	}

	private void setBooleanAttr(String key, boolean value) {
		SharedPreferences.Editor edit = preference.edit();
		edit.putBoolean(key, value);
		edit.commit();
	}

	private void setIntAttr(String key, int value) {
		SharedPreferences.Editor edit = preference.edit();
		edit.putInt(key, value);
		edit.commit();
	}
	
	
	private void setLongAttr(String key, long value) {
		SharedPreferences.Editor edit = preference.edit();
		edit.putLong(key, value);
		edit.commit();
	}
	/**
	 * 保存usercode
	 * 
	 * @param usercode
	 * @return
	 * 
	 * @author 
	 */
	public boolean setUserCode(String usercode) {
		preference = mContext.getSharedPreferences(USER_KEY, Context.MODE_PRIVATE);
		Editor editor = preference.edit();
		editor.putString(USER_CODE, usercode);
		return editor.commit();
	}

	/**
	 * 获取username
	 * 
	 * @return
	 * @author 
	 */
	public String getUserName() {
		preference = mContext.getSharedPreferences(USER_KEY, Context.MODE_PRIVATE);
		return preference.getString(USER_NAME, "");
	}
	/**
	 * 保存usercode
	 * 
	 * @param usercode
	 * @return
	 * 
	 * @author 
	 */
	public boolean setUserName(String username) {
		preference = mContext.getSharedPreferences(USER_KEY, Context.MODE_PRIVATE);
		Editor editor = preference.edit();
		editor.putString(USER_NAME, username);
		return editor.commit();
	}

	/**
	 * 获取usercode
	 * 
	 * @return
	 * @author 
	 */
	public String getUserCode() {
		preference = mContext.getSharedPreferences(USER_KEY, Context.MODE_PRIVATE);
		return preference.getString(USER_CODE, "");
	}
	
	
	
	
	/**
	 * 保存
	 * 
	 * @param Gxdwid
	 * @return
	 * 
	 * @author 
	 */
	public boolean setGxdwid(String gxdwid) {
		preference = mContext.getSharedPreferences(USER_KEY, Context.MODE_PRIVATE);
		Editor editor = preference.edit();
		editor.putString(USER_GXDWID, gxdwid);
		return editor.commit();
	}
	/**
	 * 获取usercode
	 * 
	 * @return
	 * @author 
	 */
	public String getPcsdm() {
		preference = mContext.getSharedPreferences(USER_KEY, Context.MODE_PRIVATE);
		return preference.getString(USER_PCSDM, "");
	}
	/**
	 * 保存
	 * 
	 * @param usercode
	 * @return
	 * 
	 * @author 
	 */
	public boolean setPcsdm(String pcsdm) {
		preference = mContext.getSharedPreferences(USER_KEY, Context.MODE_PRIVATE);
		Editor editor = preference.edit();
		editor.putString(USER_PCSDM, pcsdm);
		return editor.commit();
	}
	/**
	 * 获取usercode
	 * 
	 * @return
	 * @author 
	 */
	public String getPrivilegid() {
		preference = mContext.getSharedPreferences(USER_KEY, Context.MODE_PRIVATE);
		return preference.getString(USER_PRIVILEGID, "");
	}
	/**
	 * 保存
	 * 
	 * @param usercode
	 * @return
	 * 
	 * @author 
	 */
	public boolean setPrivilegid(String privilegid) {
		preference = mContext.getSharedPreferences(USER_KEY, Context.MODE_PRIVATE);
		Editor editor = preference.edit();
		editor.putString(USER_PRIVILEGID, privilegid);
		return editor.commit();
	}
	/**
	 * 获取
	 * 
	 * @return
	 * @author 
	 */
	public String getGxdwid() {
		preference = mContext.getSharedPreferences(USER_KEY, Context.MODE_PRIVATE);
		return preference.getString(USER_GXDWID, "");
	}
	/**
	 * 保存GPS定位坐标
	 * 
	 * @param lx
	 * @param ly
	 * @author 
	 * 
	 */
	public void setGpsPoint(double lx, double ly) {
		preference = mContext.getSharedPreferences(GPS_POINT_KEY, Context.MODE_PRIVATE);
		SharedPreferences.Editor edit = preference.edit();
		edit.putFloat(GPS_POINT_LX, (float) lx);
		edit.putFloat(GPS_POINT_LY, (float) ly);
		edit.commit();
	}

	/**
	 * 获取GPS定位坐标
	 * 
	 * @return
	 * @author 
	 */
	public Point getGpsPoint() {
		preference = mContext.getSharedPreferences(GPS_POINT_KEY, Context.MODE_PRIVATE);
		Point point = null;
		if ((float) 0.0 == preference.getFloat(GPS_POINT_LX, (float) 0.0)) {
			return null;
		} else {
			point = new Point();
			point.setXY(preference.getFloat(GPS_POINT_LX, (float) 0.0), preference.getFloat(GPS_POINT_LY, (float) 0.0));
		}
		return point;
	}
	
	// -------------用于缓存地图的缩放级别began---------------------
			public boolean setMapViewZoomLevel(int zoomLevel) {
				preference = mContext.getSharedPreferences(MAP_ZOOM_LEVLE_KEY, Context.MODE_PRIVATE);
				Editor editor = preference.edit();
				editor.putInt(MAP_ZOOM_LEVLE_VALUE, zoomLevel);
				return editor.commit();
			}

			public int getMapViewZoomLevel() {
				preference = mContext.getSharedPreferences(MAP_ZOOM_LEVLE_KEY, Context.MODE_PRIVATE);
				return preference.getInt(MAP_ZOOM_LEVLE_VALUE, 5);
			}
			// -------------用于缓存地图的缩放级别end---------------------
			public int getAttrInt(String key) {
				preference = PreferenceManager.getDefaultSharedPreferences(mContext);
				return preference.getInt(key, 0);
			}

			public String getString(String key) {
				preference = PreferenceManager.getDefaultSharedPreferences(mContext);
				return preference.getString(key, "");
			}
}
